package com.genesis.mall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.genesis.mall.common.PageInfo;
import com.genesis.mall.entity.SmsHomeBrand;
import com.genesis.mall.mapper.SmsHomeBrandMapper;
import com.genesis.mall.service.ISmsHomeBrandService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 首页推荐品牌表 服务实现类
 * </p>
 *
 * @author author
 * @since 2022-08-01
 */
@Service
public class SmsHomeBrandServiceImpl extends ServiceImpl<SmsHomeBrandMapper, SmsHomeBrand> implements ISmsHomeBrandService {

    @Autowired
    private SmsHomeBrandMapper brandMapper;

    //批量修改推荐品牌状态
    @Override
    public void updateRecommendStatus(List<Long> ids, Integer recommendStatus) {
        List<SmsHomeBrand> list = this.listByIds(ids);
        for (SmsHomeBrand smsHomeBrand : list) {
            smsHomeBrand.setRecommendStatus(recommendStatus);
        }
        this.updateBatchById(list);
    }

    //分页查询推荐品牌
    @Override
    public PageInfo listPage(Integer pageNum, Integer pageSize, String brandName, Integer recommendStatus) {
        Page<SmsHomeBrand> pageInfo = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<SmsHomeBrand> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(recommendStatus != null, SmsHomeBrand::getRecommendStatus, recommendStatus);
        wrapper.like(brandName != null, SmsHomeBrand::getBrandName, brandName);
        brandMapper.selectPage(pageInfo, wrapper);

        this.page(pageInfo, wrapper);
        int total = (int) pageInfo.getTotal();
        PageInfo page = new PageInfo();

        BeanUtils.copyProperties(pageInfo, page);
        List<SmsHomeBrand> brandList = pageInfo.getRecords();
        page.setList(brandList);
        page.setPageNum(pageNum);
        page.setPageSize(pageSize);
        page.setTotalPage((int) pageInfo.getPages());
        page.setTotal(total);

        return page;
    }
}
